package cn.com.taiji.pos.controller.Abandoned;

import cn.com.taiji.model.finals.SessionNames;
import cn.com.taiji.pos.entity.ConfigMealTime;
import cn.com.taiji.pos.entity.FrontHandOver;
import cn.com.taiji.pos.entity.SystemAclUser;
import cn.com.taiji.pos.entity.SystemPosMachine;
import cn.com.taiji.pos.service.IConfigMealTimeService;
import cn.com.taiji.pos.service.IFrontHandOverService;
import cn.com.taiji.pos.service.ISystemPosMachineService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.LocalDateTime;


//@RestController
//@RequestMapping("/TakeOver")
public class TakeOverController {
    @Autowired
    private ISystemPosMachineService systemPosMachineService;
    @Autowired
    private IConfigMealTimeService configMealTimeService;
    @Autowired
    private IFrontHandOverService frontendHandOverService;

    /**
     * 接班
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "TakeOver", method = RequestMethod.POST)
    public String takeOver(Model model, HttpServletRequest request, HttpServletResponse response) {
        model.addAttribute("result", "fail");
        SystemAclUser user = (SystemAclUser) WebUtils.getSessionAttribute(request, SessionNames.LOGIN_USER);
        String physicalCode = request.getParameter("physicalCode");
        String confirmAmount = request.getParameter("confirmAmount");//确认金额
        String withdrawalAmount = request.getParameter("withdrawalAmount");//提取金额
        String spareAmount = request.getParameter("spareAmount");//备用金额
        String canci = request.getParameter("canci");//餐次
        FrontHandOver frontHandOver = new FrontHandOver();
        if (!StringUtils.hasText(confirmAmount)) {
            model.addAttribute("message", "确认金额不能为空");
            return JSONObject.toJSONString(model);
        } else if (!StringUtils.hasText(withdrawalAmount)) {
            model.addAttribute("message", "提取金额不能为空");
            return JSONObject.toJSONString(model);
        } else if (!StringUtils.hasText(spareAmount)) {
            model.addAttribute("message", "备用金额不能为空");
            return JSONObject.toJSONString(model);
        }
        frontHandOver.setConfirmAmount(new BigDecimal(confirmAmount));
        frontHandOver.setExtractAmount(new BigDecimal(withdrawalAmount));
        frontHandOver.setReserveAmount(new BigDecimal(spareAmount));
        frontHandOver.setDutyPersonId(user.getId());
        SystemPosMachine posMachine = systemPosMachineService.findByPhysicalCode(physicalCode);
        if (posMachine != null) {
            frontHandOver.setPosMachineId(posMachine.getId());
            frontHandOver.setRestaurantId(posMachine.getRestaurantId());
        } else {
            frontHandOver.setPosMachineId("");
            frontHandOver.setRestaurantId("");
        }
        ConfigMealTime configMealTime = configMealTimeService.findByName(canci);
        if (configMealTime != null) {
            frontHandOver.setMealTimeId(configMealTime.getId());
        } else {
            frontHandOver.setMealTimeId("");
        }
        frontHandOver.setHandTime(LocalDateTime.now());
        frontHandOver.setReasonId("");
        frontHandOver.setIsHanded("1");
        frontHandOver.setHandAmount(BigDecimal.ZERO);
        frontHandOver.setCreatorId(user.getId());
        frontHandOver.setCreatorName(user.getName());
        frontHandOver.setCreateTime(LocalDateTime.now());
        systemPosMachineService.updateMoney(posMachine, spareAmount);
        frontendHandOverService.save(frontHandOver);
        model.addAttribute("result", "success");
        model.addAttribute("message", "接班成功");
        return JSONObject.toJSONString(model);
    }
}
